package my.mall.admin.api.controller

import jakarta.servlet.http.HttpServletResponse
import jakarta.validation.Valid
import my.mall.admin.api.config.PreAuthorize
import my.mall.admin.api.service.CategoryService
import my.mall.admin.api.service.CommonService
import my.mall.admin.api.service.GoodsService
import my.mall.admin.api.pojo.goods.GoodsDetail
import my.mall.admin.api.pojo.goods.GoodsItem
import my.mall.admin.api.pojo.goods.GoodsStatusParam
import my.mall.admin.api.pojo.login.LoginInfo
import my.mall.common.annotation.Login
import my.mall.common.annotation.Slf4j
import my.mall.common.enums.RespMetaEnum
import my.mall.common.pojo.CommonResp
import my.mall.common.pojo.PageParam
import my.mall.common.pojo.PageResp
import my.mall.common.util.RespUtils
import org.springframework.web.bind.annotation.*

@Slf4j
@RestController
@RequestMapping("goods")
class GoodsController(
    private val goodsService: GoodsService,
    private val categoryService: CategoryService,
    private val commonService: CommonService
) {

    @PreAuthorize("Goods-List")
    @GetMapping("list")
    fun getGoodsList(
        @Login loginInfo: LoginInfo,
        goodsStatus: Int?,
        page: PageParam
    ): CommonResp<PageResp<GoodsItem>> {
        return goodsService.getGoodsInfoList(loginInfo, goodsStatus, page)
    }

    @GetMapping("download")
    fun downloadGoodsList(
        @Login loginInfo: LoginInfo,
        goodsStatus: Int?,
        page: PageParam,
        httpResponse: HttpServletResponse
    ) {
        val resp = goodsService.getGoodsInfoList(loginInfo, goodsStatus, page)
        commonService.exportExcel(
            "商品列表", GOODS_TITLE, resp.data.records, httpResponse
        )
    }

    @PreAuthorize("Goods-Detail")
    @GetMapping("detail")
    fun getGoodsDetail(@Login loginInfo: LoginInfo, @RequestParam goodsId: Long?): CommonResp<GoodsDetail?> {
        return RespUtils.success(goodsService.getGoodsDetail(loginInfo.storeId, goodsId))
    }

    @GetMapping("cate")
    fun getCateList(@RequestParam(required = false) parentId: Long?): CommonResp<*> {
        return RespUtils.success(categoryService.listByPid(parentId))
    }

    @PreAuthorize("Goods-Add")
    @PostMapping("save")
    fun saveGoods(@Login loginInfo: LoginInfo, @RequestBody goodsDetail: @Valid GoodsDetail): CommonResp<*> {
        goodsService.saveGoods(loginInfo, goodsDetail)
        return RespUtils.success()
    }

    @PreAuthorize("Goods-Edit")
    @PostMapping("edit")
    fun editGoods(@Login loginInfo: LoginInfo, @RequestBody goodsDetail: @Valid GoodsDetail): CommonResp<*> {
        return if (goodsDetail.id == null) {
            RespUtils.warning<Any>(RespMetaEnum.PARAM_ERROR)
        } else goodsService.editGoods(loginInfo, goodsDetail)
    }

    @PostMapping("updateStatus")
    fun updateStatus(@Login loginInfo: LoginInfo, @RequestBody statusParam: @Valid GoodsStatusParam): CommonResp<*> {
        goodsService.updateStatus(loginInfo, statusParam)
        return RespUtils.success()
    }

    companion object {
        private val GOODS_TITLE = arrayOf(
            arrayOf("商品名称", "goodsName"),
            arrayOf("创建时间", "createdTime"),
            arrayOf("真实销量", "realSalesValue")
        )
    }
}
